home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Utilities / Programming / C Reference Card 2.3 / C Reference Card / C Reference Card.rsrc / TEXT_412_12.txt < prev    next >
Encoding:
Text File  |  1995-11-14  |  2.7 KB  |  77 lines

  1. PREPROCESSING : file inclusion, macros, conditional inclusion, pragmas
  2. _________________________________________________________________________
  3.  
  4.  
  5. FILE INCLUSION
  6.  
  7. You use the '#include' preprocessing directive to include header files.  The entire contents of the specified file is inserted in place of the file inclusion statement.  The following are some examples:
  8.  
  9.   #include <iostream.h>
  10.   #include <stdlib.h>
  11.   #include "myHeader.h"
  12.  
  13. The '<>' brackets identify standard libraries which are part of the compiler and follow implementation-defined rules to find the file.  The double-quote brackets define local files (usually within the directory or sub-directories that your project file is located in).
  14.  
  15.  
  16.  
  17. MACROS
  18.  
  19. Macros use the '#define' preprocessing directive to substitute any occurrence of the macro 'name' with the identified text in the macro definition.  Examples include:
  20.  
  21.   #define MAX_CHARS 255
  22.   #define ERROR_STR "\pError, try again."
  23.   #define MAX(A,B)  ( (A)>(B) ? (A) : (B) )
  24.  
  25.  
  26.  
  27. CONDITIONAL INCLUSION
  28.  
  29. You can use conditional inclusion preprocessing directives to define or include items which are compiler or operating system specific.
  30.  
  31.   #ifndef OS
  32.   #define OS MAC
  33.   #endif
  34.  
  35.   #if OS == MAC
  36.     #define SYS_HEADER "MacOS.h"
  37.   #elif OS == WIN
  38.     #define SYS_HEADER "Win.h"
  39.   #elif OS = WIN95
  40.     #define SYS_HEADER "Win95.h"
  41.   #elif OS = UNIX
  42.     #define SYS_HEADER "Unix.h"
  43.   #endif
  44.   #include SYS_HEADER
  45.  
  46.  
  47.  
  48. PRAGMA DIRECTIVES
  49.  
  50. Pragma directives are compiler-specific and therefore, tend to be less portable.  The format for THINK C pragma directives is:
  51.  
  52.   #pragma [SC] pragma_directive [pragma_args]
  53.  
  54. If you specify SC, the directive must be recognized by Symantec C/C++ compilers.  Some examples of THINK C pragma directives include:
  55.  
  56.   #pragma [SC] align [1/2/4]         // sets byte alignments within
  57.                                      // structures.
  58.  
  59.   #pragma [SC] message "text"        // prints text while compiling.
  60.  
  61.   #pragma [SC] once                  // when included in a header file,
  62.                                      // file is included only once even
  63.                                      // if #include directives include
  64.                                      // it multiple times.
  65.  
  66.   #pragma [SC] options align=power   // equivalent to align 4 directive.
  67.   #pragma [SC] options align=native
  68.  
  69.   #pragma [SC] options align=mac68k  // equivalent to align 2 directive.
  70.  
  71.   #pragma [SC] options align=reset   // default alignment.
  72.  
  73.   #pragma [SC] template class<args>  // produces instantiations of a
  74.                                      // template.
  75.  
  76.   #pragma [SC] template_access code  // code type can be public, extern,
  77.                                      // or static.